package com.example.wujinapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wujinapi.comm.R;
import com.example.wujinapi.entity.Firsttype;
import com.example.wujinapi.entity.Products;
import com.example.wujinapi.entity.Productspecification;
import com.example.wujinapi.entity.wxDecryptPhoneUser;
import com.example.wujinapi.service.ProductsService;
import com.example.wujinapi.service.ProductspecificationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (Products)表控制层
 *
 * @author makejava
 * @since 2023-07-10 22:08:36
 */
@RestController
@RequestMapping("products/")
public class ProductsController {
    /**
     * 服务对象
     */
    @Resource
    private ProductsService productsService;

    @Resource
    private ProductspecificationService productspecificationService;

    /**
     * 获取素有产品信息
     */
    @GetMapping("/getProducts")
    public R getProducts()  {
        try{
            List<Map<String, Object>> products =productsService.getProducts(1,100);
            return R.ok("成功").put("data",products);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            return R.error("失败");
        }
    }


    /**
     * 获取单个信息
     */
    @GetMapping("/getProductById/{id}")
    public R getProductById(@PathVariable("id") Integer id){
        try {
            Products products=productsService.getProductById(id);
            //添加输出字段
            JSONObject OutPutDatas= JSON.parseObject(JSON.toJSONString(products));
            OutPutDatas.put("specificationDatas",productspecificationService.getProductspecificationsByProductID(products.getId()));
            OutPutDatas.remove("isshow");
            OutPutDatas.remove("createtime");
            OutPutDatas.remove("psort");
            OutPutDatas.remove("ptags");
            /*OutPutDatas.remove("productvideourl");
            OutPutDatas.remove("productparas");
            OutPutDatas.remove("productselled");
            OutPutDatas.remove("productstore");
            */
            return R.ok().put("data",OutPutDatas);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(400,"失败");
        } finally {

        }
    }



    /**
     * 搜索产品
     */
    @GetMapping("/searchProductByKeyWord/{keyWord}")
    public R searchProductByKeyWord(@PathVariable("keyWord") String keyWord){
        try {
            List<Products> products=productsService.searchProducts(keyWord);
            return R.ok().put("data",products);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(400,"失败");
        }
    }



}

